package com.ycy.chuwenuniapp.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycy.chuwenuniapp.common.Constant;
import com.ycy.chuwenuniapp.common.ResCodeEnum;
import com.ycy.chuwenuniapp.common.ResData;
import com.ycy.chuwenuniapp.dto.GoodsDto;
import com.ycy.chuwenuniapp.dto.GoodsSizeDto;
import com.ycy.chuwenuniapp.entity.Goods;
import com.ycy.chuwenuniapp.entity.GoodsColor;
import com.ycy.chuwenuniapp.entity.GoodsSize;
import com.ycy.chuwenuniapp.exception.CustomException;
import com.ycy.chuwenuniapp.mapper.IGoodsMapper;
import com.ycy.chuwenuniapp.mapper.IGoodsSizeMapper;
import com.ycy.chuwenuniapp.vo.GoodsSizeVo;
import com.ycy.chuwenuniapp.vo.GoodsVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsSizeService extends ServiceImpl<IGoodsSizeMapper, GoodsSize> {
    //增加或修改
    public Boolean saveGoodsSize(String type, GoodsSize entity) {
        String goodsId = entity.getGoodsId();
        String size = entity.getSize();
        String id = entity.getId();
        boolean isAdd = StrUtil.isBlank(id);
        if(StrUtil.isBlank(goodsId)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失商品id");
        if(StrUtil.isBlank(size)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失商品尺码");

//        QueryWrapper<GoodsSize> wrapper = new QueryWrapper<>();
//        wrapper.eq("goods_id", goodsId);
//        wrapper.eq("size", size);
//        long sameNameCount = count(wrapper);
//        if (sameNameCount != 0) throw new CustomException(ResCodeEnum.BAD_REQUEST,"该商品已存在相同的尺码: " + size);

        if(Constant.ADD.equals(type)) return save(entity);
        else if(Constant.UPDATE.equals(type)) return updateById(entity);
        else throw new CustomException(ResCodeEnum.BAD_REQUEST,"saveGoodsColor方法的类型值只能是Constant.ADD或Constant.UPDATE");
    }

    //查全部
    public List<GoodsSize> findAll(String goodsId, String size) {
        QueryWrapper<GoodsSize> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(goodsId),"goods_id",goodsId);
        wrapper.eq(StrUtil.isNotBlank(size),"size",size);
        return list(wrapper);
    }

    public List<GoodsSize> findAllByIds(List<String> sizeIdList) {
        QueryWrapper<GoodsSize> wrapper = new QueryWrapper<>();
        wrapper.in("id",sizeIdList);
        return list(wrapper);
    }

    public List<GoodsSize> findAllByGoodsIds(ArrayList<String> goodsIdList) {
        QueryWrapper<GoodsSize> wrapper = new QueryWrapper<>();
        wrapper.in("goods_id",goodsIdList);
        return list(wrapper);
    }

    public Boolean delete(String goodsId) {
        if(StrUtil.isBlank(goodsId)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失商品id");
        QueryWrapper<GoodsSize> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",goodsId);
        return remove(wrapper);
    }


}
